﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using MediaPortal.Configuration;
using MediaPortal.Profile;

namespace GeckoBrowser.GUIConfiguration
{
    public partial class AdvancedConfigForm : Form
    {
        public AdvancedConfigForm()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                settings.SetValueAsBool("GeckoBrowser", "LogWarning", cbWarning.Checked);
                settings.SetValueAsBool("GeckoBrowser", "LogError", cbError.Checked);
                settings.SetValueAsBool("GeckoBrowser", "LogDebug", cbDebug.Checked);
            }
            Close();
        }

        private void AdvancedConfigForm_Load(object sender, EventArgs e)
        {
            using (Settings settings = new Settings(Config.GetFile(Config.Dir.Config, "MediaPortal.xml")))
            {
                cbWarning.Checked = settings.GetValueAsBool("GeckoBrowser", "LogWarning", true);
                cbError.Checked = settings.GetValueAsBool("GeckoBrowser", "LogError", true);
                cbDebug.Checked = settings.GetValueAsBool("GeckoBrowser", "LogDebug", false);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnOpenLogFolder_Click(object sender, EventArgs e)
        {
            string dir = Config.GetFolder(Config.Dir.Log);
            if (Directory.Exists(dir))
            {
                Process.Start(dir);
            }
            else
            {
                MessageBox.Show("Error opening " + dir + ". Directory doesn't exist. It seems that the are some MediaPortal configuration problems.", "Error opening folder!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnOpenLog_Click(object sender, EventArgs e)
        {
            string logFile = Config.GetFolder(Config.Dir.Log) + @"\geckobrowser.log";
            if (File.Exists(logFile))
            {
                Process.Start(logFile);
            }
            else
            {
                MessageBox.Show("Error opening " + logFile + ". The log file doesn't exist. Please start MediaPortal first, to show the log file.", "Error opening file!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
